﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ModularFixture.CApMFVariationGridRow
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EMPAPPBASELib;
using EMPMODELLib;
using EMPTYPELIBRARYLib;
using System.Collections;
using System.Drawing;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ModularFixture;

internal class CApMFVariationGridRow : CApMFGridRow
{
  private string m_name;
  private ArrayList m_variationOptions;
  private int m_activeOption;

  internal CApMFVariationGridRow(string name, int prototypeId, int instanceId)
  {
    this.m_variationOptions = new ArrayList();
    this.m_name = name;
    this.m_prototypeId = prototypeId;
    this.m_instanceId = instanceId;
    this.m_targetLibraryId = CApMFAdminUtils.m_targetLibraryId;
    this.SetIcon();
    this.LoadOptions();
    this.LoadAttributes();
  }

  internal override int SaveNodeToEms(EmpObjectKey parent)
  {
    this.ValidateInternalIds();
    this.UpdateVariationName();
    if (this.m_prototypeId == 0)
    {
      IEmpAppItem objectItem;
      if (CApMFAdminUtils.CreateEmsObject("UnitConfigurationVarPrototype", CApMFAdminUtils.m_targetLibraryId, out objectItem))
      {
        this.m_prototypeId = objectItem.Key.objectId;
        CApMFAdminUtils.SetObjectName(this.m_prototypeId, this.m_name);
        if (CApMFAdminUtils.ConnectEmsObject(objectItem.Key, parent))
          this.m_instanceId = CApMFAdminUtils.GetInstanceId(parent, objectItem.Key);
      }
    }
    else if (this.m_instanceId == 0 || !CApMFAdminUtils.IsDescendant(parent.objectId, this.m_instanceId))
    {
      EmpObjectKey empObjectKey;
      empObjectKey.objectId = this.m_prototypeId;
      if (CApMFAdminUtils.ConnectEmsObject(empObjectKey, parent))
        this.m_instanceId = CApMFAdminUtils.GetInstanceId(parent, empObjectKey);
    }
    for (int index1 = 0; index1 < this.m_variationOptions.Count; ++index1)
    {
      IEmpAppItem objectItem;
      if (((CApMFVariationOption) this.m_variationOptions[index1]).PrototypeId == 0 && CApMFAdminUtils.CreateEmsObject("UnitConfigurationPrototype", CApMFAdminUtils.m_targetLibraryId, out objectItem))
      {
        ((CApMFVariationOption) this.m_variationOptions[index1]).PrototypeId = objectItem.Key.objectId;
        CApMFAdminUtils.SetObjectName(((CApMFVariationOption) this.m_variationOptions[index1]).PrototypeId, ((CApMFVariationOption) this.m_variationOptions[index1]).Name);
        EmpObjectKey empObjectKey;
        empObjectKey.objectId = this.m_prototypeId;
        CApMFAdminUtils.ConnectEmsObject(objectItem.Key, empObjectKey);
        ((CApMFVariationOption) this.m_variationOptions[index1]).InstanceId = CApMFAdminUtils.GetInstanceId(empObjectKey, objectItem.Key);
      }
      if (index1 != this.m_activeOption)
      {
        ArrayList gridRows = ((CApMFVariationOption) this.m_variationOptions[index1]).GridRows;
        ArrayList parentGridRows = ((CApMFVariationOption) this.m_variationOptions[index1]).ParentGridRows;
        Hashtable hashtable = new Hashtable();
        for (int index2 = 0; index2 < gridRows.Count; ++index2)
        {
          EmpObjectKey parent1;
          if (parentGridRows[index2] is ArrayList)
          {
            CApMFVariationGridRow variationGridRow = (CApMFVariationGridRow) ((ArrayList) parentGridRows[index2])[0];
            parent1.objectId = variationGridRow.GetInternalIdForOptionName(((ArrayList) parentGridRows[index2])[1].ToString());
          }
          else
            parent1.objectId = ((CApMFGridRow) parentGridRows[index2]).PrototypeId;
          ((CApMFGridRow) gridRows[index2]).SaveNodeToEms(parent1);
          if (hashtable.ContainsKey((object) parent1.objectId))
            ((ArrayList) hashtable[(object) parent1.objectId]).Add((object) ((CApMFGridRow) gridRows[index2]).InstanceId);
          else
            hashtable.Add((object) parent1.objectId, (object) new ArrayList()
            {
              (object) ((CApMFGridRow) gridRows[index2]).InstanceId
            });
        }
        IDictionaryEnumerator enumerator = hashtable.GetEnumerator();
        while (enumerator.MoveNext())
          CApMFAdminUtils.RemoveDeletedObjects((int) enumerator.Key, (ArrayList) enumerator.Value);
      }
    }
    this.SaveAttributes();
    this.m_targetLibraryId = CApMFAdminUtils.m_targetLibraryId;
    return ((CApMFVariationOption) this.m_variationOptions[this.m_activeOption]).PrototypeId;
  }

  private void UpdateVariationName()
  {
    if (this.m_prototypeId != 0)
      CApMFAdminUtils.SetObjectName(this.m_prototypeId, this.m_name);
    if (this.m_instanceId == 0)
      return;
    CApMFAdminUtils.SetObjectName(this.m_instanceId, this.m_name);
  }

  internal void RemoveDeletedOptions()
  {
    ArrayList childrenIdsList = new ArrayList();
    for (int index = 0; index < this.m_variationOptions.Count; ++index)
      childrenIdsList.Add((object) ((CApMFVariationOption) this.m_variationOptions[index]).InstanceId);
    CApMFAdminUtils.RemoveDeletedObjects(this.m_prototypeId, childrenIdsList);
  }

  internal void AddVariationOption(int prototypeId, int instanceId, string name)
  {
    this.m_variationOptions.Add((object) new CApMFVariationOption(prototypeId, instanceId, name));
  }

  internal void SetOptionRows(ArrayList parentRows, ArrayList gridRows, ArrayList parentGridRows)
  {
    if (this.m_activeOption <= -1)
      return;
    ((CApMFVariationOption) this.m_variationOptions[this.m_activeOption]).ParentRows = parentRows;
    ((CApMFVariationOption) this.m_variationOptions[this.m_activeOption]).GridRows = gridRows;
    ((CApMFVariationOption) this.m_variationOptions[this.m_activeOption]).ParentGridRows = parentGridRows;
  }

  protected override void LoadAttributes()
  {
  }

  protected override void SaveAttributes()
  {
  }

  internal int GetInternalIdForOptionName(string optionName)
  {
    int indexByOptionName = this.GetIndexByOptionName(optionName);
    int internalIdForOptionName = -1;
    if (indexByOptionName > -1)
      internalIdForOptionName = ((CApMFVariationOption) this.m_variationOptions[indexByOptionName]).PrototypeId;
    return internalIdForOptionName;
  }

  internal void SetActiveOption(string optionName)
  {
    int indexByOptionName = this.GetIndexByOptionName(optionName);
    if (indexByOptionName <= -1)
      return;
    this.m_activeOption = indexByOptionName;
  }

  internal int GetActiveOptionIndex() => this.m_activeOption;

  internal void SetActiveOption(int index) => this.m_activeOption = index;

  internal string GetActiveOption()
  {
    return ((CApMFVariationOption) this.m_variationOptions[this.m_activeOption]).Name;
  }

  internal void GetGridRows(string variationOptionName, ref ArrayList rows, ref ArrayList gridRows)
  {
    int indexByOptionName = this.GetIndexByOptionName(variationOptionName);
    if (indexByOptionName <= -1)
      return;
    rows = ((CApMFVariationOption) this.m_variationOptions[indexByOptionName]).ParentRows;
    gridRows = ((CApMFVariationOption) this.m_variationOptions[indexByOptionName]).GridRows;
  }

  internal void ValidateActiveOption()
  {
    if (this.m_activeOption < this.m_variationOptions.Count)
      return;
    this.m_activeOption = 0;
  }

  private int GetIndexByOptionName(string optionName)
  {
    int indexByOptionName = -1;
    for (int index = 0; index < this.m_variationOptions.Count; ++index)
    {
      if (((CApMFVariationOption) this.m_variationOptions[index]).Name.Equals(optionName))
      {
        indexByOptionName = index;
        break;
      }
    }
    return indexByOptionName;
  }

  private void SetIcon()
  {
    string name = this.GetType().Assembly.GetName().Name + ".ModularFixture.ConfigurationAdministrator.Res.VariationType.bmp";
    this.m_icon = (Image) new Bitmap(this.GetType().Assembly.GetManifestResourceStream(name));
  }

  private void LoadOptions()
  {
    if (this.m_prototypeId > 0)
    {
      EmpContext empContext;
      empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
      EmpToolPrototypeClass toolPrototypeClass = new EmpToolPrototypeClass();
      EmpToolInstanceClass toolInstanceClass = new EmpToolInstanceClass();
      EmpObjectKey empObjectKey1;
      empObjectKey1.objectId = this.m_prototypeId;
      foreach (EmpObjectKey child in toolPrototypeClass.GetChildren(ref empContext, ref empObjectKey1))
      {
        EmpObjectKey empObjectKey2;
        empObjectKey2.objectId = child.objectId;
        EmpObjectKey empObjectKey3 = toolInstanceClass[ref empContext, ref empObjectKey2];
        string name = toolInstanceClass[ref empContext, ref empObjectKey3];
        this.AddVariationOption(empObjectKey3.objectId, child.objectId, name);
      }
    }
    else
      this.AddVariationOption(0, 0, "Variation_1");
    this.m_activeOption = 0;
  }

  internal override string Name
  {
    get => this.m_name;
    set => this.m_name = value;
  }

  internal ArrayList VariationOptions
  {
    get => this.m_variationOptions;
    set => this.m_variationOptions = value;
  }
}
